package source.module.carousel;

import source.kernel.Container;
import source.kernel.serialization.json.Json;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Hai Thomson
 */
public class Carousel_host {
	public static void run() throws IOException, SQLException {
		// String hostid = Container.app().request.getParameter("hostid");

		String hostid = "0";
		Map host = (Map) Container.app().request.getServletContext().getAttribute("hostid");
		if (host != null && host.get("hostid") == null) {
			hostid = (String) host.get("hostid");
		}

		host = ((source.table.hosts) Container.table("hosts")).fetchNextHost(hostid);
		if (host == null || host.get("hostid") == null) {
			return;
		}

		List templates = ((source.table.hosts) Container.table("hosts")).fetchHostTemplate(host.get("hostid").toString());
		if (templates != null && templates.size() > 0) {
			host.put("template", templates);
		} else {
			host.put("template", new ArrayList());
		}

		Container.app().response.getWriter().print(Json.toJson(host));
		Container.app().response.getWriter().flush();
	}
}
